import { classicBeh } from '../classicBeh.js'

const mMgr = wx.getBackgroundAudioManager()

Component({
  behaviors: [classicBeh],
  /**
   * 组件的属性列表
   */
  properties: {
    src: String,
    title: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    isPlay: false,
    pauseSrc: './images/pause.png',
    playSrc: './images/play.png'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handlePlay (e) {
      this.setData({
        isPlay: !this.data.isPlay
      })

      if (this.data.isPlay) {
        mMgr.src = this.properties.src
        mMgr.title = this.properties.title
      } else {
        mMgr.pause()
      }
    },

    _recoverStatus () {
      if (mMgr.paused) {
        this.setData({
          isPlay: false
        })
      } else if (mMgr.src === this.properties.src) {
        this.setData({
          isPlay: true
        })
      } else {
        // 不处理
      }
    },

    _monitorSwitch () {
      mMgr.onPlay(() => {
        this._recoverStatus()
      })
      mMgr.onPause(() => {
        this._recoverStatus()
      })
      mMgr.onStop(() => {
        this._recoverStatus()
      })
      mMgr.onEnded(() => {
        this._recoverStatus()
      })
    }
  },

  attached (e) {
    // this._recoverStatus()
    this._monitorSwitch()
  },

  detached (e) {
    // mMgr.stop()
  }
})
